package com.travel.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianfeng.util.ResultMsg;
import com.travel.entity.TbAir;
import com.travel.mapper.AirMapper;
import com.travel.service.AirService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AirServiceImpl extends ServiceImpl<AirMapper,TbAir> implements AirService {

    @Autowired
    AirMapper airMapper;

    @Override
    public ResultMsg findAll() {
        List<TbAir> list = super.list();
        if(list==null){
            return  ResultMsg.error(401,"当前没有航班");

//            throw new RuntimeException("当前没有航班");
        }
        return ResultMsg.success(list);
    }

    @Override
    public ResultMsg findOne(Integer airId) {
        TbAir byId = super.getById(airId);
        if(byId==null){
            return  ResultMsg.error(402,"航线暂未开设航班");
//            throw new RuntimeException("航线暂未开设航班");
        }else {
            return ResultMsg.success(byId);
        }
    }

    @Override
    public ResultMsg findbytime(String time) {
        List<TbAir> airList=airMapper.selectBytime(time);
        if (airList!=null){
            return ResultMsg.success(airList);
        }
        ///throw new RuntimeException("该时间没开设航班");
        return  ResultMsg.error(403,"该时间没开设航班");
    }

    @Override
    public ResultMsg findbyaddr(String saddr) {
        List<TbAir> list=airMapper.selectByaddr(saddr);
        if (list!=null){
           return ResultMsg.success(list);
        }
        //throw new RuntimeException("该城市没开设航班");
        return  ResultMsg.error(404,"该城市没开设航班");
    }

    @Override
    public ResultMsg findbyaddress(String saddr, String eaddr) {
        List<TbAir> list=airMapper.selectByaddress(saddr,eaddr);
        if (list!=null){

            return ResultMsg.success(list);
        }
        //throw new RuntimeException("该城市没开设到目的地的航班");
        return  ResultMsg.error(405,"该城市没开设到目的地的航班");
    }

    @Override
    public ResultMsg findbyaddbytime(String saddr, String eaddr, String time) {
        List<TbAir> list=airMapper.selectByaddBytime(saddr,eaddr,time);
        if (list!=null){
            return ResultMsg.success(list);
        }
        //throw new RuntimeException("该城市没开设到目的地的航班");
        return  ResultMsg.error(406,"该时间本城市没开设到目的地的航班");
    }

    @Override
    public ResultMsg seletAll(int current, int size) {
        Page<TbAir> gpage = new Page<>(current,size);
        Page<TbAir> page = super.page(gpage);
        return ResultMsg.success(page);
    }

    @Override
    public ResultMsg delete(Integer id) {
        boolean b = super.removeById(id);
        if(b){
            return ResultMsg.success();
        }
        return ResultMsg.error(407,"删除失败");
    }

    @Override
    public ResultMsg modify(TbAir tbAir) {
            boolean b = super.saveOrUpdate(tbAir);
            if(b) {
                return ResultMsg.success();
            }
            return ResultMsg.error(408,"修改失败");

    }

    @Override
    public ResultMsg getAirById(int airId) {
        TbAir byId = super.getById(airId);
        if(byId==null){
            return  ResultMsg.error(409,"没有当前的飞机班次");
        }else {
            return ResultMsg.success(byId);
        }
    }


}
